<script setup>
import {ref} from "vue";
import { useRouter, useRoute } from "vue-router";
import {ElLoading, ElMessage} from "element-plus";
import {http} from "@/api";
const apiKey = 'Banner'
const router = useRouter()
const route = useRoute()
const id = route.params.id
const formData = ref({
  id: undefined,
  "image": "",
  "link": "",
  "region": 'home',
  "status": 0,
  "name": "",
  details: ''
})
const rules = ref({
  name: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  image: [
    { required: true, message: '请上传', trigger: 'blur' },
  ],
})

const regionList = [
  {
    name: '首页',
    id: 'home'
  },
  {
    name: '产业政策',
    id: 'other'
  },
]

const formRef =  ref(null)
if (id) {
  // 获取详情
  http[apiKey].read(id).then(res => {
    for (const valueKey in formData.value) {
      formData.value[valueKey] = res.data[valueKey]
    }
    formData.value.id = id
  })
}
const cancel = () => {
  router.back()
}
const confirm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      const loadingInstance = ElLoading.service()
      if (id) {
        // 编辑
        http[apiKey].update({
          ...formData.value,
          id
        }).then(res => {
          ElMessage.success('编辑成功')
          router.back()
        }).finally(() => {
          loadingInstance.close()
        })
      } else {
        // 新增
        http[apiKey].create(formData.value).then(res => {
          ElMessage.success('新增成功')
          router.back()
        }).finally(() => {
          loadingInstance.close()
        })
      }
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<template>
  <PageMain :title="id ? '编辑' :'新增'">
    <div class="form-content f-c j-b">
      <div class="form-box form-box2">
        <el-form ref="formRef" style="width: 660px" :model="formData" :rules="rules" label-position="top" >
         <el-row :gutter="20">
           <el-col :span="24">
             <el-form-item label="名称" prop="name">
               <el-input clearable v-model="formData.name"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="24">
             <el-form-item label="图片" style="width: 100%" prop="image">
               <UploadImage v-model:image-url="formData.image"/>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="展示区域" prop="region">
               <el-select v-model="formData.region" placeholder="请选择">
                 <el-option v-for="item in regionList" :label="item.name" :value="item.id" :key="item.id"></el-option>
               </el-select>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="展示状态">
               <el-switch :active-value="0" :inactive-value="1" v-model="formData.status"/>
             </el-form-item>
           </el-col>
           <el-col :span="24">
             <el-form-item label="跳转链接">
               <el-input clearable v-model="formData.link"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="24">
             <el-form-item label="内容">
               <Editor v-model="formData.content"></Editor>
             </el-form-item>
           </el-col>
         </el-row>
        </el-form>
      </div>
      <div class="form-button f-s">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </div>
    </div>
  </PageMain>
</template>
<style lang="scss">
.form-box2 {
  display: flex;
  .gap {
    width: 32px;
  }
  .avatar-uploader {
    width: 100%;
  }
  .avatar-uploader .avatar {
    width: 100%;
    height: 120px;
    display: block;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }
  .el-upload {
    width: 100%;
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100%;
    height: 120px;
    text-align: center;
  }
}
</style>
